package com.guqj.thread.design;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

public class MyBlockQueue<E> {

	private Queue<E> queue;
	private int limit;

	public MyBlockQueue(int limit) {
		this.limit = limit;
		queue = new ArrayBlockingQueue<E>(limit);
	}

	public synchronized void put(E e) throws InterruptedException {
		while(queue.size() == limit){
			wait();
		}
		queue.add(e);
		notifyAll();
	}
	
	public synchronized E take() throws InterruptedException{
		while(queue.isEmpty()){
			wait();
		}
		E e = queue.poll();
		notifyAll();
		return e;
	}
}
